package com.jtlrm.ckd.mvp.view.adapter;

import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.base.sdk.util.EditInputFilter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.jtlrm.ckd.R;
import com.jtlrm.ckd.entity.YinShiDetailEntity;
import com.jtlrm.ckd.entity.event.YinShiDeleteMessage;
import com.jtlrm.ckd.util.EventBusUtil;

public class YinShiDetailSubAdapter extends BaseQuickAdapter<YinShiDetailEntity, BaseViewHolder> {

    private boolean isEdit;

    public YinShiDetailSubAdapter(boolean isEdit) {
        super(R.layout.yinshi_detail_sub_item);
        this.isEdit = isEdit;
    }


    @Override
    protected void convert(final BaseViewHolder helper, final YinShiDetailEntity item) {
        ImageView deleteI = helper.getView(R.id.delete);
        EditText contentE = helper.getView(R.id.content);
        contentE.setEnabled(isEdit);
        if (isEdit) {
            deleteI.setVisibility(View.VISIBLE);
            deleteI.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    YinShiDeleteMessage message = new YinShiDeleteMessage();
                    message.setYinShiId(item.getId());
                    EventBusUtil.postEvent(message);
                }
            });
            InputFilter[] filters = {new EditInputFilter(mContext)};
            contentE.setFilters(filters);
        } else {
            deleteI.setVisibility(View.GONE);
        }
        helper.setText(R.id.title, item.getFoodName());
        helper.setText(R.id.content, item.getGram());

        contentE.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s != null && !TextUtils.isEmpty(s.toString()))
                    item.setGram(s.toString());
            }
        });
    }

}

